package com.tul.exercise.base.thread.base;

import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;

/**
 * 任务休眠一段时间
 */
public class SleepLightOff implements Runnable{

    // 计数器
    protected int countDown = 10;

    // 任务计数器，记录任务的数量
    private static int taskCount = 0;

    // 任务id
    private final int id = taskCount++;

    public SleepLightOff() {

    }

    public SleepLightOff(int countdown) {
        this.countDown = countdown;
    }

    public String status() {
        return "#" + id + "(" + (countDown > 0 ? countDown : "Light Off") + "),";
    }

    @Override
    public void run() {
        try {
            while (countDown-- > 0) {
                System.out.println(status() + LocalDateTime.now());

                // 休眠3S
                TimeUnit.MICROSECONDS.sleep(3000000);
                //TimeUnit.SECONDS.sleep(3)
                //Thread.sleep(3000);
            }

        } catch (InterruptedException e) {
            System.out.println(e);
        }

    }
}
